package jmesql.sqlplus;

import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.jmesql.io.Properties;
import br.com.dreamsource.mobile.jmesql.jdbcConnection;
import br.com.dreamsource.mobile.jmesql.jdbcDriver;
import br.com.dreamsource.mobile.jmesql.jdbcPreparedStatement;
import br.com.dreamsource.mobile.jmesql.jdbcResultSet;
import java.util.Vector;

/**
 * author Elizeu Nogueira da Rosa Jr.
 * version 0.1
 * 
 */

public class DatabaseManager {

   /* 
    * the database connection
    */
    private static jdbcConnection connection;

    /*
     * Properties contains ("user", "password", "database")
     */
    private static Properties props;
    
    public DatabaseManager(String databaseName, String userName, String passwd) throws SQLException {
        //Setting the properties
        props = new Properties();
        props.setProperty("user", userName);
        props.setProperty("password", passwd);
        props.setProperty("database", databaseName);
	
        if (connection == null) {
            jdbcDriver driver = new jdbcDriver();

	    /*
	     * If the database does not exists, 
	     * the dbms create a new, default 
	     * user name is "SA" and 
	     * password is empty 
	    */

            connection = driver.connect(props);
        }
    } 
    
    private jdbcConnection getConnection() throws SQLException {
        if (connection == null) {
            throw new SQLException("The connection is null!");
        }
        return connection;
    }
    
    private jdbcPreparedStatement getPreparedStatement(String sqlValue) throws SQLException {
        return this.getConnection().prepareStatement(sqlValue);
    }
    
    /*
    * Execute de SELECT statement
    * Ex: String sqlValue = "SELECT * FROM MYTABLE"
    *     jdbcResultSet resultSet = myDatabaseManager.list(sqlValue);   
    */
    public jdbcResultSet list(String sql) throws SQLException {
        jdbcPreparedStatement prepared = this.getPreparedStatement(sql);
        return prepared.executeQuery();
    }

    /*
    * Execute de SELECT statement based in Vector attributes
    * Ex: String sqlValue = "SELECT * FROM MYTABLE WHERE ID = ?"
    *     Vector values = new Vector();
    *     values.addElement("1");
    *     jdbcResultSet resultSet = myDatabaseManager.find(sqlValue, values);   
    */
    public jdbcResultSet find(String sqlValue, Vector values) throws SQLException {
        jdbcPreparedStatement preparedStatement = this.getPreparedStatement(sqlValue);
        for (int i = 0; i < values.size(); i++) {
            preparedStatement.setString(i + 1, (String)values.elementAt(i));
        }        
	return preparedStatement.executeQuery();
    }

    /*
    * Execute de INSERT statement based in Vector attributes
    * Ex: String sqlValue = "INSERT INTO MYTABLE (ID, NAME) VALUES (?, ?)"
    *     Vector values = new Vector();
    *     values.addElement("1");
    *     values.addElement("aaa");
    *     myDatabaseManager.insert(sqlValue, values);   
    */
    public boolean insert(String sqlValue, Vector values) throws SQLException {
        return this.executeUpdateSQL(sqlValue, values);
    }

    /*
    * Execute de UPDATE statement based in Vector attributes
    * Ex: String sqlValue = "UPDATE MYTABLE SET ID = ?, NAME = ? WHERE ID = ?"
    *     Vector values = new Vector();
    *     values.addElement("2");
    *     values.addElement("aaa");
    *     values.addElement("1");
    *     myDatabaseManager.update(sqlValue, values);   
    */
    public boolean update(String sqlValue, Vector values) throws SQLException {
        return this.executeUpdateSQL(sqlValue, values);
    }

    /*
    * Execute de UPDATE statement based in Vector attributes
    * Ex: String sqlValue = "DELETE FROM WHERE ID = ?"
    *     Vector values = new Vector();
    *     values.addElement("2");
    *     myDatabaseManager.delete(sqlValue, values);   
    */
    public boolean delete(String sqlValue, Vector values) throws SQLException {
        return this.executeUpdateSQL(sqlValue, values);
    }

    private boolean executeUpdateSQL(String sqlValue, Vector values) throws SQLException {
        jdbcPreparedStatement preparedStatement = this.getPreparedStatement(sqlValue);
        for (int i = 0; i < values.size(); i++) {
            preparedStatement.setString(i + 1, (String)values.elementAt(i));
        }
        if (preparedStatement.executeUpdate() > 0) {
            return true;
        } else {
            return false;
        }
    }
    
}
